var express = require("express");
var router = express.Router();
var db = require("../model/db");
var multiparty=require("multiparty")

// 体验分享
router.get("/tablist", (req, res) => {
  const sql = "select * from myExperience";
  db.query(sql, (err, result) => {
    console.log(result, "result", "查询的结果");
    if (err) {
      res.status(500).send("查询错误"); // 发生错误时返回错误信息
    } else {
      res.send(result); // 返回查询到的数据
    }
  });
});
router.post("/tabadd", (req, res) => {
  const body = req.body;
  // console.log(body, "body", "添加的结果");
  // console.log(body.face);
  const sql = `insert into myExperience(text,times,face) values('${body.text}','${body.times}', '${body.face}')`;
  db.query(sql, (err, result) => {
    if (err) {
      res.send({ code: 201, msg: "添加失败" }); // 发生错误时返回错误信息
    } else {
      res.send({ code: 200, msg: "添加成功" }); // 返回查询到的数据
    }
  });
});
router.post("/deltab", (req, res) => {
  const id = req.body.id;
  const sql = `delete from myExperience where id=${id}`;
  db.query(sql, (err, result) => {
    if (err) {
      res.send({ code: 201, msg: "删除失败" }); // 发生错误时返回错误信息
    } else {
      res.send({ code: 200, msg: "删除成功" }); // 返回查询到的数据
    }
  });
});

// 上传图片接口
router.post("/uploads",(req,res)=>{
  const form=new multiparty.Form()
  form.uploadDir='upload'
  form.parse(req,(err,fileData,imgData)=>{
    // console.log(imgData,'imgData');
    console.log("http://localhost:3100/" + imgData.file[0].path);
    res.send({
      code: 200,
      path: "http://localhost:3100/" + imgData.file[0].path,
    });
  })
})


// 企业服务--绑定企业等部分
router.get("/myEnterprise", (req, res) => {
  const sql = `select myEnterprise.ename AS enterpriseName, myEnterprise.contacts, myEnterprise.phone, myEnterprise.address, mybqi.name AS bqiName FROM myEnterprise  LEFT JOIN mybqi ON myEnterprise.cid = mybqi.id`;
  db.query(sql, (err, results) => {
    console.log(results, "results");
    if (err) {
      res.status(500).send("查询错误"); // 发生错误时返回错误信息
    } else {
      res.send(results); // 返回查询到的数据
    }
  });
});

router.get("/mybqi", (req, res) => {
  const sql = `select * from mybqi`;
  db.query(sql, (err, results) => {
    if (err) {
      return res.send({ code: 201, msg: "查询失败" });
    } else {
      return res.send({ code: 200, msg: "查询成功", data: results });
    }
  });
});

router.post("/addEnterprise", (req, res) => {
  const body = req.body;
  // console.log(body);
  const sql = `INSERT INTO myEnterprise (ename, contacts, phone,settle, address,face,cid) values ('${body.ename}','${body.contacts}','${body.phone}','${body.address}','${body.face}','${body.settle[0]}','${body.cid}')`;
  db.query(sql, (err, result) => {
    console.log(result, "result", "企业添加成功");
    if (err) {
      res.send({ code: 201, msg: "添加失败" }); // 发生错误时返回错误信息
    } else {
      res.send({ code: 200, msg: "添加成功" }); // 返回查询到的数据
    }
  });
});


router.post("/delEnterprise", (req, res) => {
  const id = req.body.id; // 获取前端传递的id参数
  const sql = `delete from myEnterprise where id = ${id}`;
  db.query(sql, (err, result) => {
    if (err) {
      res.send({ code: 201, msg: "删除失败" });
    } else {
      res.send({ code: 200, msg: "删除成功" });
    }
  });
});





module.exports = router;
